package main

import (
	"Nacos_GateWay/config"
	"Nacos_GateWay/controller"
	"Nacos_GateWay/logcat"
	"Nacos_GateWay/middle"
	"Nacos_GateWay/nacos"
	"github.com/gin-gonic/gin"
	"log"
	"os"
)

func main() {
	//初始化读取配置工具
	conf := config.LoadConfig("")
	//初始化日志工具
	logcat.InitLog(conf.GetString("log.path"), conf.GetString("log.name"), false)
	//判断日志文件目录是否创建
	_, fileErr := os.Stat(conf.GetString("log.path"))
	if os.IsNotExist(fileErr) {
		//文件夹不存在创建
		createErr := os.Mkdir(conf.GetString("log.path"), os.ModePerm)
		if createErr != nil {
			log.Println("##文件创建失败")
		}
	}
	//nacos
	nacos.Run(conf)
	//初始化数据库
	//db.InitDB(conf)
	////gin
	r := gin.Default()
	r.Use(middle.LogerMiddleware())
	r.Use(gin.Recovery())
	//使用中间件鉴权
	api := r.Group("/api", controller.JWTAuth)
	{
		api.POST("/*name", controller.ReverseProxy)
		api.GET("/*name", controller.ReverseProxy)
		api.PUT("/*name", controller.ReverseProxy)
		api.DELETE("/*name", controller.ReverseProxy)
		api.PATCH("/*name", controller.ReverseProxy)
	}
	r.Run(":" + conf.GetString("web.port"))
}
